package tmt.usercenter.web.vo.user;

import com.tmt.spring.validation.RequiredField;
import lombok.Getter;
import lombok.Setter;
import org.springframework.util.StringUtils;
import tmt.usercenter.web.domain.TmtUser;
import tmt.usercenter.web.domain.enums.IdCardType;

import javax.persistence.Column;
import java.io.Serializable;
import java.text.SimpleDateFormat;
import java.util.Date;

@Getter
@Setter
public class RegisterVo implements Serializable {

	/** 用户头像, 提供默认头像 */
//	@RequiredField(label = "用户头像")
	private String avatar;

	/** 用户名 */
	@RequiredField(label = "用户帐号", order = 1, repeatable = false)
	private String username;

	/** 密码 */
	@RequiredField(label = "帐号密码", order = 2)
	private String password;

	/** 密码确认 */
	@RequiredField(label = "密码确认", order = 3, methodParam = {"password"})
	private String verify;

	/** 用户昵称 */
	@RequiredField(label = "用户昵称", order = 6)
	private String nickname;

	/** 真实姓名 */
	@RequiredField(label = "真实姓名", order = 7)
	private String realName;

	/** 证件类型, 是否必填由idCardNum字段决定 */
	@RequiredField(label = "证件类型", order = 8)
	private IdCardType cardType = IdCardType.ID_CARD_2;

	/** 身份证号码 */
	@RequiredField(label = "身份证号", order = 9, methodName = "*")
	private String idCardNum;

	/** 出生日期 是否必填由cardType字段决定，如果cardType = IdCardType.ID_CARD_2，则不需要填写本字段 */
	@RequiredField(label = "出生日期", order = 10, methodParam = {"idCardNum"})
	private Date birthday;

	/** 电话号码 */
	@RequiredField(label = "手机号码", order = 11)
	private String mobile;

	/** 绑定邮箱，绑定后可用邮箱登录, 且可以找回密码 */
	@RequiredField(label = "绑定邮箱", order = 12)
	private String email;

	/**
	 * 第三方系统编号
	 */
	@RequiredField(label = "外部系统", order = 13, display = false)
	private String clientId;

	/**
	 * 外部系统中会员的唯一编码
	 */
	private Long externalCode;

	/**
	 * 在外部系统中会员推荐人的唯一编码。
	 */
	private Long introducerCode;

	/**
	 * 指定会员的默认角色。自动导入会员功能使用该字段。多角色之间使用逗号间隔。
	 */
	private String defaultRoles;


	public TmtUser convertToUser(String defaultAvatar, String defaultUsername, String defaultPasswrod) {
		TmtUser user = new TmtUser();
		user.setAvatar(!StringUtils.isEmpty(this.getAvatar()) ? this.getAvatar() : defaultAvatar);
		user.setUsername(!StringUtils.isEmpty(this.getUsername()) ? this.getUsername() : defaultUsername);
		user.setPassword(!StringUtils.isEmpty(this.getPassword()) ? this.getPassword() : defaultPasswrod);
		user.setNickname(this.getNickname());
		user.setRealName(this.getRealName());
		user.setCardType(this.getCardType());
		user.setIdCardNum(this.getIdCardNum());
		user.setMobile(this.getMobile());
		user.setEmail(this.getEmail());
		user.setExternalCode(this.getExternalCode());
		user.setIntroducerCode(this.getIntroducerCode());

		user.setBirthday(this.getBirthday());
		if (this.getBirthday() == null
				&& !StringUtils.isEmpty(this.getIdCardNum())
				&& this.getCardType() == IdCardType.ID_CARD_2) {
			try {
				SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
				Date birthday = sdf.parse(this.getIdCardNum().substring(6, 14));
				user.setBirthday(birthday);
			} catch (Exception ex) {
			}
		}

		return user;
	}
}
